<?php

class HistoryController extends Zend_Controller_Action{
    private $_paginator;
	public function init()
    {
        
        $acl = new My_Acl();
        $auth = $acl->checkAllowed($this->_request);
        
        
    	$this->_paginator = array('itemCountPerPage' => 10, 'pageRange' => 5);
		$this->_config = new Zend_Config_Ini(CONFIG_PATH . '/columns.ini',APPLICATION_ENV);
    }
    public function indexAction()
    {
		$model_history = new Default_Model_History();
		$page = $this->_getParam('page',1);
		$option = array('page'=>$page,'count'=>$this->_paginator['itemCountPerPage']);
		$history = $model_history->getsHistory($option);		
		$this->view->alias = $this->_config->alias;
		//paging
		$total = $model_history->countItems();
		$paginator= new My_Paginator();
		$this->view->paginator = $paginator->createPaginator($total, $this->_paginator['itemCountPerPage'], $this->_paginator['pageRange'], $page);
		//end paging
		$this->view->history = $history;
    }
	public function searchAction()
	{
		$startDate		= strtotime($this->_getParam('startDate', date("Y/m/d", time() - 86400)));					
		$endDate 		= strtotime($this->_getParam('endDate', date("Y/m/d", time())));				
		$sltFill_add 		= $this->_getParam('sltFill_add',0);				
		$sltFill_project 		= $this->_getParam('sltFill_project',0);				
		$sltFill_category 		= $this->_getParam('sltFill_category',0);
		$search 		= $this->_getParam('search', '');
        
		$this->view->startDate = $startDate;
		$this->view->endDate = $endDate;
		$this->view->sltFill_add = $sltFill_add;
		$this->view->sltFill_project = $sltFill_project;
		$this->view->sltFill_category = $sltFill_category;
		//nếu không có hành động search thì không search	
		//thông tin paging
		$page 	= $this->_getParam('page',1);
		$option = array('page'=>$page,'count'=>$this->_paginator['itemCountPerPage']);
		//end thông tin paging
		
		//get result tìm kiếm	
		$model_history 	= new Default_Model_History();
		$history = $model_history->searchHistory($startDate, $endDate, $sltFill_add, $sltFill_project, $sltFill_category, $option);
        $count = $model_history->countHistory($startDate, $endDate, $sltFill_add, $sltFill_project, $sltFill_category);
		$this->view->history = $history;
		$this->view->alias = $this->_config->alias;
		//end get result tìm kiếm
		$paginator	= new My_Paginator();
		$this->view->paginator = $paginator->createPaginator($count['Total'], $this->_paginator['itemCountPerPage'], $this->_paginator['pageRange'], $page);
		
	}
	
	public function exportexcelAction()
	{
	    Zend_Layout::getMvcInstance()->disableLayout();
	    $this->_helper->viewRenderer->setNoRender();	   
		$startDate		= $this->_getParam('startDate','');					
		$endDate 		= $this->_getParam('endDate'.'');			
		$sltFill_add 		= $this->_getParam('sltFill_add',0);				
		$sltFill_project 		= $this->_getParam('sltFill_project',0);				
		$sltFill_category 		= $this->_getParam('sltFill_category',0);	
		$search 		= $this->_getParam('search', '');                
		$model_history 	= new Default_Model_History();
        $helper_time    = $this->_helper->time;
        $alias          = $this->_config->alias->toArray();
		$option['excel'] = 1;
		$arrData = $model_history->searchHistory($startDate, $endDate, $sltFill_add, $sltFill_project, $sltFill_category, $option)->toArray();
        
        if($arrData){
            error_reporting(E_ERROR | E_WARNING | E_PARSE);
            $workbook = My_Pear::getInstanceSpreadsheet();
    	    $workbook->setVersion(8, 'utf-8'); 
    		$worksheet1 = &$workbook->addWorksheet('History '.date("dmY")); 
            $worksheet1->setInputEncoding('utf-8');
            $worksheet1->setColumn(0,0,6);
            $worksheet1->setColumn(1,1,15);
            $worksheet1->setColumn(2,3,12);
            $worksheet1->setColumn(4,6,30);
            $worksheet1->setColumn(7,7,12);
            
    		//style title
    		$header = &$workbook->addFormat(); 
    		$header->setBold(); 
    		$header->setColor('white'); 
    		$header->setFgColor('grey'); 
    		$header->setHAlign('center'); 
    		$header->setVAlign('center'); 
    		$header->setBorder(1); 	
            $header->setTextWrap();
    
    		$textStyle = &$workbook->addFormat(); 
    		$textStyle->setAlign('vcentre');
    		$textStyle->setHAlign('left');
            $textStyle->setBorder(1);
            $textStyle->setTextWrap();
    		
    		$numberFormat = &$workbook->addFormat();
    		$numberFormat->setHAlign('right');
    		$numberFormat->setNumFormat('#,###');
            $numberFormat->setBorder(1);
            $numberFormat->setTextWrap();
    		//end style		
            
    		$temp = array();
    		for($i = 0; $i<count($arrData); $i++){
    			array_unshift($arrData[$i], $i +1);
    			unset($arrData[$i]['equipmentId'], $arrData[$i]['historyId']);
    		}	
            	
    		$config = $this->_config;
            
			//lấy danh sách title
			$arrCols = array_keys($arrData[0]);			
			//rename title
			for($j = 1; $j<count($arrCols); ++$j){
				if(isset($config->export->history->$arrCols[$j]))
					$arrCols[$j] = $config->export->history->$arrCols[$j];				
			}			
			foreach($arrCols as $key=>$title){
				if($title == "0"){
					$title = "STT";
				}
				$worksheet1->write(0, $key, $title, $header); 
			}
            
			//lấy danh sách title
			$worksheet1->write(0, 0, 'STT', $header); 
			$worksheet1->write(0, 1, 'Mã thiết bị', $header); 
			$worksheet1->write(0, 2, 'Hành động', $header); 
			$worksheet1->write(0, 3, 'Người cập nhật', $header); 
			$worksheet1->write(0, 4, 'Thuộc tính cập nhật', $header); 
			$worksheet1->write(0, 5, 'Giá trị cũ', $header); 
			$worksheet1->write(0, 6, 'Giá trị mới', $header); 
			$worksheet1->write(0, 7, 'Ngày cập nhật', $header); 
			$worksheet1->write(0, 8, 'Item Name', $header); 
			$worksheet1->write(0, 9, 'Giá hiện tại(Đã VAT)', $header);
			//end title
            
            //Ghi dữ liệu history
            $stt = 1;
            $num_row = 1;
            foreach($arrData as $row){
                $row['priceNow'] = (int)(str_replace(array(".",","), array("",""), $row['priceNow']));
                                        
                $worksheet1->writeString($num_row, 0, $stt, $textStyle);
                $worksheet1->writeString($num_row, 1, $row['equipmentName'], $textStyle);
                $worksheet1->writeString($num_row, 2, $row['action'], $textStyle);
                $worksheet1->writeString($num_row, 3, $row['username'], $textStyle);
                $worksheet1->writeString($num_row, 7, $helper_time->dataToDate($row['date']), $textStyle);
                $worksheet1->writeString($num_row, 8, $row['itemName'], $textStyle);
                $worksheet1->writeNumber($num_row, 9, $row['priceNow'], $numberFormat);
                
                $row_equipmentField = json_decode($row['equipmentField']);
                $row_valueOld = json_decode($row['valueOld']);
                $row_valueNew = json_decode($row['valueNew']);
                $count = count($row_equipmentField);
                if($count){
                    if($count > 1){
                        $worksheet1->setMerge($num_row, 0, $num_row+$count-1, 0);
                        $worksheet1->setMerge($num_row, 1, $num_row+$count-1, 1);
                        $worksheet1->setMerge($num_row, 2, $num_row+$count-1, 2);
                        $worksheet1->setMerge($num_row, 3, $num_row+$count-1, 3);
                        $worksheet1->setMerge($num_row, 7, $num_row+$count-1, 7);
                    }
                    foreach($row_equipmentField as $key=>$value){
                        $worksheet1->writeString($num_row, 4, $alias[$row_equipmentField[$key]], $textStyle);
                        $worksheet1->writeString($num_row, 5, $row_valueOld[$key], $textStyle);
                        $worksheet1->writeString($num_row, 6, $row_valueNew[$key], $textStyle);
                        ++$num_row;
                    }
                }else{
                    $worksheet1->writeString($num_row, 4, "---", $textStyle);
                    $worksheet1->writeString($num_row, 5, "---", $textStyle);
                    $worksheet1->writeString($num_row, 6, "---", $textStyle);
                    ++$num_row;
                }
                ++$stt;
            }
            
    		$name = "cmdb_history_".date("dmY");
    		$workbook->send("{$name}.xls"); 
    		$workbook->close();
            
        }elseif (!is_array($arrData) || empty($arrData)){	
			echo 'Dữ liệu không có! Vui lòng kiểm tra lại!';
			exit(1);
		}	
	}
}







